


package jnpf.fico.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jnpf.base.ActionResult;
import jnpf.base.UserInfo;
import jnpf.base.entity.VisualdevEntity;
import jnpf.base.vo.PageListVO;
import jnpf.base.vo.PaginationVO;
import jnpf.engine.entity.FlowTaskEntity;
import jnpf.engine.enums.FlowStatusEnum;
import jnpf.exception.DataException;
import jnpf.exception.WorkFlowException;
import jnpf.fico.entity.ZcglCwBillChangeEntity;
import jnpf.fico.model.zcglcwbillchange.ZcglCwBillChangeForm;
import jnpf.fico.model.zcglcwbillchange.ZcglCwBillChangeInfoVO;
import jnpf.fico.model.zcglcwbillchange.ZcglCwBillChangeListVO;
import jnpf.fico.model.zcglcwbillchange.ZcglCwBillChangePagination;
import jnpf.fico.service.ZcglCwBillChangeService;
import jnpf.permission.entity.UserEntity;
import jnpf.util.GeneraterSwapUtil;
import jnpf.util.JsonUtil;
import jnpf.util.RandomUtil;
import jnpf.util.UserProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 *
 * zcglCwBillChange
 * @版本： V3.2.0
 * @版权： 智果云科技（https://www.jnpfsoft.com）
 * @作者： JNPF开发平台组
 * @日期： 2024-12-11
 */
@Slf4j
@RestController
@Api(tags = "zcglCwBillChange" , value = "example")
@RequestMapping("/api/ZcglCwBillChange")
public class ZcglCwBillChangeController {

    @Autowired
    private GeneraterSwapUtil generaterSwapUtil;


    @Autowired
    private UserProvider userProvider;

    @Autowired
    private ZcglCwBillChangeService zcglCwBillChangeService;



    /**
     * 列表
     *
     * @param zcglCwBillChangePagination
     * @return
     */
    @ApiOperation("获取列表")
	@PostMapping("/getList")
    public ActionResult list(@RequestBody ZcglCwBillChangePagination zcglCwBillChangePagination)throws IOException{
        List<ZcglCwBillChangeEntity> list= zcglCwBillChangeService.getList(zcglCwBillChangePagination);

        //处理id字段转名称，若无需转或者为空可删除
    List<ZcglCwBillChangeListVO> listVO=JsonUtil.getJsonToList(list,ZcglCwBillChangeListVO.class);
    for(ZcglCwBillChangeListVO zcglCwBillChangeVO:listVO){
                zcglCwBillChangeVO.setCreatoruserid_name(zcglCwBillChangeVO.getCreatoruserid());

                zcglCwBillChangeVO.setCreatortime_name(zcglCwBillChangeVO.getCreatortime());

                zcglCwBillChangeVO.setRemarks_name(zcglCwBillChangeVO.getRemarks());

		//子表数据转换
     }

		for(ZcglCwBillChangeListVO vo:listVO){
            FlowTaskEntity flowTaskEntity = generaterSwapUtil.getInfoSubmit(vo.getId(), FlowTaskEntity::getStatus);
            if (flowTaskEntity!=null){
            vo.setFlowState(flowTaskEntity.getStatus());
            }else{
            vo.setFlowState(null);
            }
		}
         PageListVO vo=new PageListVO();
         vo.setList(listVO);
         PaginationVO page=JsonUtil.getJsonToBean(zcglCwBillChangePagination,PaginationVO.class);
         vo.setPagination(page);
         return ActionResult.success(vo);

    }

    /**
     * 创建
     *
     * @param crData
     * @return
     */
    @PostMapping
    @Transactional
    @ApiOperation("创建")
    public ActionResult create(@RequestBody  Map<String,Object> crData) throws DataException {
        ZcglCwBillChangeForm zcglCwBillChangeForm = JsonUtil.getJsonToBean(crData,ZcglCwBillChangeForm.class);
	boolean b =  zcglCwBillChangeService.checkForm(zcglCwBillChangeForm,0);
        if (b){
         return ActionResult.fail("单行输入不能重复");
        }
        String visualId ="636544885754706757";
        VisualdevEntity visualdevEntity =generaterSwapUtil.getVisualEntity(visualId);
        String mainId =RandomUtil.uuId();
        UserInfo userInfo=userProvider.get();
		UserEntity userEntity = generaterSwapUtil.getUser(userInfo.getUserId());
        ZcglCwBillChangeEntity entity = JsonUtil.getJsonToBean(zcglCwBillChangeForm, ZcglCwBillChangeEntity.class);
            entity.setId(mainId);
        entity.setCreatoruserid(userProvider.get().getUserId());
        entity.setCreatortime(new Date());
        zcglCwBillChangeService.save(entity);

        if (FlowStatusEnum.submit.getMessage().equals(zcglCwBillChangeForm.getStatus())){
            try {
                generaterSwapUtil.submitFlowTask(visualdevEntity,mainId,zcglCwBillChangeForm,userInfo,zcglCwBillChangeForm.getCandidateList(),zcglCwBillChangeForm.getFlowUrgent());
            } catch (WorkFlowException e) {
                e.printStackTrace();
            }
                return ActionResult.success("提交成功，请耐心等待");
        }
        return ActionResult.success("保存成功");
}


    /**
    * 信息
    *
    * @param id
    * @return
    */
    @ApiOperation("信息")
    @GetMapping("/{id}")
    public ActionResult<ZcglCwBillChangeInfoVO> info(@PathVariable("id") String id){
        ZcglCwBillChangeEntity entity= zcglCwBillChangeService.getInfo(id);
        ZcglCwBillChangeInfoVO vo=JsonUtil.getJsonToBean(entity, ZcglCwBillChangeInfoVO.class);

		//子表
		//副表
        return ActionResult.success(vo);
    }





   /**
    * 更新
    *
    * @param id
    * @param upData
    * @return
    */
    @PutMapping("/{id}")
    @Transactional
    @ApiOperation("更新")
    public ActionResult update(@PathVariable("id") String id,@RequestBody @Valid Map<String,Object> upData) throws DataException {
        ZcglCwBillChangeForm zcglCwBillChangeForm = JsonUtil.getJsonToBean(upData,ZcglCwBillChangeForm.class);
	boolean b =  zcglCwBillChangeService.checkForm(zcglCwBillChangeForm,1);
        if (b){
         return ActionResult.fail("单行输入不能重复");
        }
        String visualId ="636520813998127109";
        VisualdevEntity visualdevEntity =generaterSwapUtil.getVisualEntity(visualId);
        UserInfo userInfo=userProvider.get();
        ZcglCwBillChangeEntity entity= zcglCwBillChangeService.getInfo(id);
        if(entity!=null){
            ZcglCwBillChangeEntity subentity=JsonUtil.getJsonToBean(zcglCwBillChangeForm, ZcglCwBillChangeEntity.class);
				boolean b1 = zcglCwBillChangeService.updateById(subentity);
				if (!b1){
				return ActionResult.fail("当前表单原数据已被调整，请重新进入该页面编辑并提交数据");
				}
        if (FlowStatusEnum.submit.getMessage().equals(zcglCwBillChangeForm.getStatus())){
            try {
					generaterSwapUtil.submitFlowTask(visualdevEntity,id,subentity,userInfo,zcglCwBillChangeForm.getCandidateList(),zcglCwBillChangeForm.getFlowUrgent());
            } catch (WorkFlowException e) {
                e.printStackTrace();
            }
             return ActionResult.success("提交成功，请耐心等待");
        }
        return ActionResult.success("更新成功");
        }else{
            return ActionResult.fail("更新失败，数据不存在");
        }
    }

   /**
    * 删除
    *
    * @param id
    * @return
    */
    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    @Transactional
    public ActionResult delete(@PathVariable("id") String id){
        ZcglCwBillChangeEntity entity= zcglCwBillChangeService.getInfo(id);
        if(entity!=null){
                FlowTaskEntity taskEntity = generaterSwapUtil.getInfoSubmit(id, FlowTaskEntity::getId, FlowTaskEntity::getStatus);
            if (taskEntity != null) {
                try {
				generaterSwapUtil.deleteFlowTask(taskEntity);
                } catch (WorkFlowException e) {
                    e.printStackTrace();
                }
            }
            zcglCwBillChangeService.delete(entity);

        }
        return ActionResult.success("删除成功");
    }


}
